package spring3.controller;
 
//import net.viralpatel.spring3.form.Contact;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import spring3.form.Contact;
import spring3.form.ContactForm;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import webservice.Servicio_ejem2ServiceStub;
import webservice.Servicio_ejem2ServiceStub.Restar;
import webservice.Servicio_ejem2ServiceStub.RestarResponse;
import webservice.Servicio_ejem2ServiceStub.Sumar;
import webservice.Servicio_ejem2ServiceStub.SumarResponse;
import webservice.Servicio_ejemploServiceStub;
import webservice.Servicio_ejemploServiceStub.AreaCirculo;
import webservice.Servicio_ejemploServiceStub.AreaCirculoResponse;
import webservice.Servicio_ejemploServiceStub.PerimetroCirculo;
import webservice.Servicio_ejemploServiceStub.PerimetroCirculoResponse;

 
@Controller
@SessionAttributes
public class ContactController {
 
    @RequestMapping(value = "/addContact", method = RequestMethod.POST)
    public ModelAndView addContact(@ModelAttribute("contact") @Valid  Contact contact, BindingResult result) {
         //BindingResult result, 
    	 if(result.hasErrors()) {
    		 System.out.println("ERROR");
             return new ModelAndView("contact");
         }
        
        try {
        	
        	Servicio_ejem2ServiceStub oStub2= new Servicio_ejem2ServiceStub();
    		// AGREGA
    		
    		int n1 = Integer.parseInt(contact.getFirstname());
    		int n2 = Integer.parseInt(contact.getLastname());
    		
    		Servicio_ejem2ServiceStub.Sumar sum = new Servicio_ejem2ServiceStub.Sumar();
			Servicio_ejem2ServiceStub.Restar res = new Servicio_ejem2ServiceStub.Restar();
			
			sum.setNum(n1);
			sum.setNum2(n2);
            
			res.setNum(n1);
			res.setNum2(n2);
    	   	
    		
            // AreaCirculoResponse objResponse = oStub.areaCirculo(oAgregarContacto);
            
            SumarResponse objResponse = oStub2.sumar(sum);
    		float msj = objResponse.get_return();
    		String mensaje = "";
    		mensaje += "La suma de "+n1+" y "+n2+" es : "+msj+"\n";
    	
    		//PerimetroCirculoResponse objResponse2 = oStub.perimetroCirculo(oPerimetroCirculo);
    		RestarResponse objResponse2 = oStub2.restar(res);
    		float msj2 = objResponse2.get_return();
    		mensaje += "La resta de "+n1+" y "+n2+" es : "+msj2+"\n";
		        System.out.println(mensaje);
				return new ModelAndView("saludo", "message", mensaje);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return new ModelAndView("error", "message", "ERROR");
		}
         
        //return "redirect:contacts.html";
        
    }
     
    @RequestMapping("/contacts")
    public ModelAndView showContacts() {
         
        return new ModelAndView("contact", "command", new Contact());
    }
    
    /*
    
    @RequestMapping("/show")
	public ModelAndView helloWorld() {
    	//String message = "Hola Mundo Spring se ha instaldo correctamente";
    	 List<Contact> contacts = new ArrayList<Contact>();
    	 
    	    
    	        contacts.add(new Contact("Barack", "Obama", "barack.o@whitehouse.com", "147-852-965"));
    	        contacts.add(new Contact("George", "Bush", "george.b@whitehouse.com", "785-985-652"));
    	        contacts.add(new Contact("Bill", "Clinton", "bill.c@whitehouse.com", "236-587-412"));
    	        contacts.add(new Contact("Ronald", "Reagan", "ronald.r@whitehouse.com", "369-852-452"));
    	    
    	 ContactForm contactForm = new ContactForm();
         
         try {

     		ServicioContactoStub oStub = new ServicioContactoStub();
        	 MostrarContacto oMostrarContacto = new MostrarContacto();
     		
     		MostrarContactoResponse objResponde =  oStub.mostrarContacto(oMostrarContacto);
     		ContactoVO[] contacts2= objResponde.get_return();
     		
			 contactForm.setContacts(contacts2);
	         return new ModelAndView("showcontactrow" , "contactForm", contactForm);
	         
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return new ModelAndView("error", "message", "ERROR");
		}
        
	}*/

}